package com.hurui.xiaoming.fragment.creditor;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.hurui.xiaoming.R;
import com.hurui.xiaoming.adapter.CreditorListAdapter;
import com.hurui.xiaoming.bean.Li_CreditorResult;
import com.hurui.xiaoming.bean.Li_MyList_Result;
import com.hurui.xiaoming.fragment.BaseFragment;
import com.hurui.xiaoming.myconfig.PamaryKeys;
import com.hurui.xiaoming.utils.DefultData;
import com.hurui.xiaoming.utils.WriteReadSharedPrefs;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by hurui on 2017/7/21.
 */

public class CreditorFragment extends BaseFragment{
    private static int INIT_PAGE = 1;
    private View rootView ;
    private XRecyclerView creaditor_rv ;
    private List<Li_MyList_Result.DataBean> lists ;
    private CreditorListAdapter adapter ;
    private String oi_state;
    private BottomSheetDialog mBottomSheetDialog;
    private Map<String, String> map=new HashMap<>();

    int page = INIT_PAGE;

    public static CreditorFragment newInstance(String str) {
        Bundle args = new Bundle();

        CreditorFragment fragment = new CreditorFragment();
        args.putString("oi_state", str);
        Log.i("ljb_oi_state",str);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        oi_state = getArguments().getString("oi_state");
        Log.i("ljb_oi_state1",oi_state);
        page = INIT_PAGE;
        if(rootView == null){
            rootView = inflater.inflate(R.layout.fragment_creaditor , null);
            lists = new ArrayList<Li_MyList_Result.DataBean>();
            initView();
        }
        initAction();
        return rootView ;
    }


    @Override
    public void initView() {
        creaditor_rv = (XRecyclerView) rootView.findViewById(R.id.creaditor_rv);
        creaditor_rv.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new CreditorListAdapter(mContext);
        creaditor_rv.setAdapter(adapter);

        mBottomSheetDialog = new BottomSheetDialog(mContext);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i("ljb","onStart");
        getIntentData("",page);
    }

    @Override
    public void initAction() {

        creaditor_rv.setLoadingListener(new XRecyclerView.LoadingListener() {
            //todo 下拉加载
            @Override
            public void onRefresh() {
                Log.i("ljb","onRefresh");
                getIntentData("",INIT_PAGE);
            }

            // todo 下拉刷新
            @Override
            public void onLoadMore() {
                Log.i("ljb","onLoadMore");
                page++;
                getIntentData("",page);
            }
        });
    }
    /*public void loadData(){
        map.put(PamaryKeys.UI_ID, ""+ WriteReadSharedPrefs.readUserID(mContext));
        ////map.put(PamaryKeys.UI_ID, "1");
        map.put("oi_state", oi_state);
        apiSevice.onHTttp_myOrder(map).enqueue(new Callback<Li_MyList_Result>() {
            @Override
            public void onResponse(Call<Li_MyList_Result> call, Response<Li_MyList_Result> response) {
                Li_MyList_Result body = response.body();
                List<Li_MyList_Result.DataBean> data = body.getData();
                adapter.refreshData(data);
                creaditor_rv.refreshComplete();
                creaditor_rv.loadMoreComplete();
//                Toast.makeText(getActivity(),"请求成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<Li_MyList_Result> call, Throwable t) {
                Log.i("异常",t.getMessage());
                onToast(t.getMessage() + "00000000000");
                creaditor_rv.refreshComplete();
                creaditor_rv.loadMoreComplete();
            }
        });

    }*/

    //获取网络数据
    public void getIntentData(String oi_num,int page){
        Log.i("ljb",oi_state + "---" + WriteReadSharedPrefs.readUserID(mContext));
        map.clear();
        map.put(PamaryKeys.UI_ID, ""+ WriteReadSharedPrefs.readUserID(mContext));
//         map.put(PamaryKeys.UI_ID, "1");
        map.put("oi_num",oi_num);
        map.put("ui_type", "5");
        map.put("oi_state",oi_state);
        map.put("page_num",String.valueOf(page));
        Log.i("ljb",map.toString() +  getArguments().toString());
        Call<Li_CreditorResult> call = apiSevice.onHttpPostCreditorData(DefultData.indexData,map);
        call.enqueue(new Callback<Li_CreditorResult>() {
            @Override
            public void onResponse(Call<Li_CreditorResult> call, Response<Li_CreditorResult> response) {
                if(response.isSuccessful()){
                    Li_CreditorResult li_financeResult = response.body();

                    List<Li_CreditorResult.DataBean> data = li_financeResult.getData();
                    if(data.size() >= 1){
                        adapter.refreshData(data,oi_state);

                        Log.e("ljb", "onResponse: "+data.toString() );
                    }else{
                        //onToast("失败111");
                    }
                    creaditor_rv.refreshComplete();
                    creaditor_rv.loadMoreComplete();

//                Toast.makeText(getActivity(),"请求成功",Toast.LENGTH_SHORT).show();

                }else{
                    creaditor_rv.refreshComplete();
                    creaditor_rv.loadMoreComplete();

                   onToast("失败000");
                }
            }

            @Override
            public void onFailure(Call<Li_CreditorResult> call, Throwable t) {
                onHttpFail( -1 , t.getMessage());
                creaditor_rv.refreshComplete();
                creaditor_rv.loadMoreComplete();
                onToast("失败333");
            }
        });
    }

    /**
     * 搜索输入
     */

    private void searchText() {

        final View view = LayoutInflater.from(mContext).inflate(R.layout.update_name_address, null, false);
        final EditText myName = (EditText) view.findViewById(R.id.myHobby);
        final TextView textYES = (TextView) view.findViewById(R.id.myOK);

        //点击确认按钮
        textYES.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!TextUtils.isEmpty(myName.getText().toString())) {
                    getIntentData(myName.getText().toString().trim(),page);
                }
                mBottomSheetDialog.dismiss();

            }
        });

        createBottomSheetDialog(view);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                showKeyboard(myName);
            }  }, 90);
    }

    /**
     * 底部滑动显示
     *
     */
    private void createBottomSheetDialog( View view) {

        //获取要添加到BottomSheetDialog中的布局view

        mBottomSheetDialog.setContentView(view);
        //显示
        mBottomSheetDialog.show();
    }

    /**
     * 显示软键盘
     *
     */
    public void showKeyboard(EditText editText) {
        if(editText!=null){
            //设置可获得焦点
            editText.setFocusable(true);
            editText.setFocusableInTouchMode(true);
            //请求获得焦点
            editText.requestFocus();
            //调用系统输入法
            InputMethodManager inputManager = (InputMethodManager) editText
                    .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.showSoftInput(editText, 0);
        }
    }


}
